package com.naiterui.ehp.bs.doctor.vo.licence;

import static com.naiterui.ehp.bp.domain.Licence.*;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.domain.Licence;
import com.naiterui.ehp.bs.doctor.utils.ImageUrlUtil;
import com.naiterui.ehp.bs.doctor.vo.IdCardVO;
import com.naiterui.ehp.bs.doctor.vo.LicenceVO;
import com.naiterui.ehp.bs.doctor.vo.MedicalLicenseVO;
import com.naiterui.ehp.bs.doctor.vo.QualificationCertificateVO;

/**
 * 职业资格证VO工厂
 *
 * @author Amber
 * @since 1.0.0
 */
public class LicenceVOFactory {

    /**
     * 从Licence->LicenceVO
     * LicenceVOFactory.createFromLicence()
     *
     * @param licence
     *
     * @return
     *
     * @Author Amber
     * @Date 2017年7月31日
     * @since 1.0.0
     */
    public static LicenceVO createFromLicence(Licence licence) {
        if (licence == null) {
            return null;
        }
        // 资质类型
        Integer type = licence.getType();
        switch (type) {
            case DOCTOR_LICENCE_TYPE_IDCARD:
                // 身份证
                IdCardVO idCardVO = new IdCardVO();
                initLicenceVO(idCardVO, licence);
                idCardVO.setNumber(licence.getNumber());
                return idCardVO;
            case DOCTOR_LICENCE_TYPE_MEDICAL:
                // 职业资格证
                MedicalLicenseVO medicalLicenseVO = new MedicalLicenseVO();
                initLicenceVO(medicalLicenseVO, licence);
                medicalLicenseVO.setLicenseNumber(licence.getNumber());
                // medicalLicenseVO.setVersionType(licence.getVersionType());
                return medicalLicenseVO;

            case DOCTOR_LICENCE_TYPE_QUALIFICATION_CERTIFICATE:
                //医师资格证
                QualificationCertificateVO certificateVO = new QualificationCertificateVO();
                certificateVO.setVersionType(licence.getVersionType());
                certificateVO.setQuaNumber(licence.getNumber());
                initLicenceVO(certificateVO, licence);
                return certificateVO;
            default:
                // 其他资质，照片、工作证、职称证书
                LicenceVO licenceVO = new LicenceVO();
                initLicenceVO(licenceVO, licence);
                return licenceVO;
        }

    }

    /**
     * 从资质实体初始化资质VO基本信息
     * LicenceVOFactory.initLicenceVO()
     *
     * @param licenceVO
     * @param licence
     *
     * @Author Amber
     * @Date 2017年7月31日
     * @since 1.0.0
     */
    private static void initLicenceVO(LicenceVO licenceVO, Licence licence) {
        try {
            if (licenceVO == null || licence == null) {
                return;
            }
            licenceVO.setType(licence.getType());
            Integer licenceId = licence.getId();
            licenceVO.setId(licenceId == null ? null : licenceId.longValue());
            licenceVO.setDoctorId(licence.getDoctorId());
            licenceVO.setNumber(licence.getNumber());
            String urls = licence.getUrl();
            if (StringUtils.isEmpty(urls)) {
                // log异常数据
                return;
            }

            String[] urlArray = urls.trim().split(",");
            List<String> urlList = Lists.newArrayList();
            for (String url : urlArray) {
                urlList.add(ImageUrlUtil.getImagePath(url));
            }

            licenceVO.setUrls(urlList);
        } catch (Exception e) {
            // TODO: Log

        }

    }

}
